package com.first.core.util;

import java.util.Map;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.VelocityException;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.ui.velocity.VelocityEngineUtils;

public class MailEngine {
	private final Log log = LogFactory.getLog(MailEngine.class);
	private MailSender mailSender;
	private VelocityEngine velocityEngine;
	private String defaultFrom;
	private String turnOn;

	public void setMailSender(MailSender mailSender) {
		this.mailSender = mailSender;
	}

	public void setVelocityEngine(VelocityEngine velocityEngine) {
		this.velocityEngine = velocityEngine;
	}

	public void setFrom(String from) {
		this.defaultFrom = from;
	}

	public void sendMessage(SimpleMailMessage msg, String templateName,
			Map model) {
		String result = null;
		try {
			result = VelocityEngineUtils.mergeTemplateIntoString(
					this.velocityEngine, templateName, model);
		} catch (VelocityException e) {
			e.printStackTrace();
			this.log.error(e.getMessage());
		}

		msg.setText(result);
		send(msg);
	}

	public void send(SimpleMailMessage msg) throws MailException {
		try {
			if ("true".equalsIgnoreCase(this.turnOn))
				this.mailSender.send(msg);
			else
				this.log.warn("邮件发送开关未打开：" + msg.getTo() + " " + msg.getText());
		} catch (MailException ex) {
			this.log.error(ex.getMessage());
			throw ex;
		}
	}

	public void sendMessage(String[] recipients, String sender,
			ClassPathResource resource, String bodyText, String subject,
			String attachmentName) throws MessagingException {
		MimeMessage message = ((JavaMailSenderImpl) this.mailSender)
				.createMimeMessage();

		MimeMessageHelper helper = new MimeMessageHelper(message, true);

		helper.setTo(recipients);

		if (sender == null)
			helper.setFrom(this.defaultFrom);
		else {
			helper.setFrom(sender);
		}

		helper.setText(bodyText, true);
		helper.setSubject(subject);

		helper.addAttachment(attachmentName, resource);

		if ("true".equalsIgnoreCase(this.turnOn))
			((JavaMailSenderImpl) this.mailSender).send(message);
		else
			this.log.warn("邮件发送开关未打开：" + recipients + " " + bodyText);
	}

	public void sendMessage(String recipients, String sender, String bodyText,
			String subject) throws MessagingException {
		MimeMessage message = ((JavaMailSenderImpl) this.mailSender)
				.createMimeMessage();

		MimeMessageHelper helper = new MimeMessageHelper(message, true);

		helper.setTo(recipients);

		if (sender == null)
			helper.setFrom(this.defaultFrom);
		else {
			helper.setFrom(sender);
		}

		helper.setText(bodyText, true);
		helper.setSubject(subject);

		if ("true".equalsIgnoreCase(this.turnOn))
			((JavaMailSenderImpl) this.mailSender).send(message);
		else
			this.log.warn("邮件发送开关未打开：" + recipients + " " + bodyText);
	}

	public void sendMessage(String recipients, String subject,
			String templateName, Map model) throws MessagingException {
		MimeMessage message = ((JavaMailSenderImpl) this.mailSender)
				.createMimeMessage();

		MimeMessageHelper helper = null;
		if ((recipients == null)
				|| (recipients.toLowerCase().indexOf("@js11183.com") == -1))
			helper = new MimeMessageHelper(message, true, "UTF-8");
		else {
			helper = new MimeMessageHelper(message, true, "GB2312");
		}

		helper.setTo(recipients);

		String result = null;
		try {
			result = VelocityEngineUtils.mergeTemplateIntoString(
					this.velocityEngine, templateName, "UTF-8", model);
		} catch (VelocityException e) {
			e.printStackTrace();
			this.log.error(e.getMessage());
		}
		helper.setText(result, true);
		helper.setSubject(subject);
		helper.setFrom(this.defaultFrom);

		if ("true".equalsIgnoreCase(this.turnOn))
			((JavaMailSenderImpl) this.mailSender).send(message);
		else
			this.log.warn("邮件发送开关未打开：" + recipients + " " + result);
	}

	public void setTurnOn(String turnOn) {
		this.turnOn = turnOn;
	}
}